<template>
  <div>
    <!-- 两句诗 -->
    <div class="my-animation-slide-top">
      <twoPoem/>
    </div>

    <div style="background: var(--background);padding-top: 40px;" class="my-animation-slide-bottom">
      <!-- 标签 -->
      <div class="sort-warp shadow-box" v-if="!$common.isEmpty(tag)">
        <div v-for="(v, index) in tag" :key="index"
             :class="{isActive: !$common.isEmpty(v.labelId)}"
             @click="labelArticle(v.labelId)">
          <proTag :info="v.labelName"
                  :color="$constant.before_color_list[Math.floor(Math.random() * 6)]"
                  style="margin: 12px">
          </proTag>
        </div>
      </div>

      <!-- 文章 -->
      <div class="article-wrap">

        <div v-if="!$common.isEmpty(articles)" class="recent-post-container">
          <div class="article-first">
            <svg viewBox="0 0 1024 1024" width="20" height="20" style="vertical-align: -2px;">
              <use xlink:href="#icon-faxian_dianji"/>
            </svg>
            发现
          </div>

          <div class="recent-post-item shadow-box background-opacity"
               v-for="(article, index) in articles"
               :key="index"
               @click="clickArticle(article)">
            <!-- 封面 -->
            <div class="recent-post-item-image" :class="{ leftImage: index % 2 !== 0, rightImage: index % 2 === 0 }">
              <el-image class="my-el-image"
                        v-once
                        lazy
                        :src="article.articleCover"
                        fit="cover">
                <div slot="error" class="image-slot myCenter" style="background-color: var(--lightGreen)">
                  <div class="error-text">
                    <div>遇事不决，可问春风</div>
                  </div>
                </div>
              </el-image>
            </div>
            <!-- 内容 -->
            <div class="recent-post-item-post" :class="{ leftImage: index % 2 === 0, rightImage: index % 2 !== 0 }">
              <!-- 时间 -->
              <div class="post-meta">
                <svg class="icon" aria-hidden="true">
                  <use xlink:href="#icon-rili"/>
                </svg>
                发布于 {{ article.createTime }}
              </div>
              <!-- 标题 -->

              <el-tooltip placement="top" effect="light">
                <div slot="content">{{ article.articleTitle }}</div>
                <h3>{{ article.articleTitle }}</h3>
              </el-tooltip>

              <!-- 信息 -->
              <div class="post-meta" style="margin-bottom: 15px">
                <span>
                    <svg class="icon" aria-hidden="true">
                      <use xlink:href="#icon-redu"/>
                    </svg>
                    {{ article.viewCount }} 热度
                  </span>

                <span>
                    <svg class="icon" aria-hidden="true">
                      <use xlink:href="#icon-yidianzan"/>
                    </svg> {{ article.likeCount }} 点赞
                  </span>

                <span>
                    <svg class="icon" aria-hidden="true">
                      <use xlink:href="#icon-pinglun"/>
                    </svg>
                    {{ article.commentCount }} 条评论
                </span>

                <span v-if="article.encoderStatus">
                    <svg class="icon" aria-hidden="true">
                      <use xlink:href="#icon-mima"/>
                    </svg>
                    已加密
                  </span>

                <span v-if="!article.encoderStatus">
                    <svg class="icon" aria-hidden="true">
                      <use xlink:href="#icon-jiesuo"/>
                    </svg>
                    未加密
                  </span>
              </div>

              <!-- 内容 -->
              <div class="recent-post-desc">
                {{ article.articleContent }}
              </div>
              <!-- 分类 标签 -->
              <div class="sort-label">
                <span style="margin-right: 12px"
                      @click.stop="$router.push({path: '/articlePage', query: {sortId: article.sortId}})">
                  <svg class="icon" aria-hidden="true">
                    <use xlink:href="#icon-fenlei"/>
                  </svg>
                  {{ article.sortName }}
                </span>
                <span
                  @click.stop="$router.push({path: '/articlePage', query: {sortId: article.sortId, labelId: article.labelId}})">
                  <svg class="icon" aria-hidden="true">
                      <use xlink:href="#icon-biaoqian1"/>
                  </svg>
                  {{ article.labelName }}
                </span>
              </div>
            </div>
          </div>
        </div>

        <div class="pagination-wrap">
          <div @click="pageArticles()" class="pagination" v-if="pagination.total !== articles.length">
            下一页
          </div>
          <div v-else style="user-select: none">
            ~~到底啦~~
          </div>
        </div>
      </div>
      <!-- 页脚 -->
      <my-footer/>
    </div>

    <div>
      <el-dialog title="密码"
                 :modal="false"
                 :visible.sync="showPasswordDialog"
                 width="25%"
                 :append-to-body="true"
                 destroy-on-close
                 center>
        <div>
          <div>
            <div class="password-content"> 请输入密码</div>
          </div>
          <div style="margin: 20px auto">
            <el-input maxlength="30" v-model="password"></el-input>
          </div>
          <div style="display: flex;justify-content: center">
            <proButton :info="'提交'"
                       @click.native="submitPassword()"
                       :before="$constant.before_color_2"
                       :after="$constant.after_color_2">
            </proButton>
          </div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
const twoPoem = () => import( "./twoPoem.vue");
const proTag = () => import( "../common/proTag");
const myFooter = () => import( "../common/footer.vue");

export default {
  components: {
    twoPoem,
    proTag,
    myFooter
  },

  data() {
    return {
      sortId: this.$route.query.sortId,
      labelId: this.$route.query.labelId,
      articleTitle: this.$route.query.articleTitle,
      pagination: {
        current: 1,
        size: 10,
        total: 0,
        sortId: this.$route.query.sortId,
        labelId: this.$route.query.labelId,
        articleTitle: this.$route.query.articleTitle,
      },
      articles: [],
      tag: [],
      showPasswordDialog: false,
      id: null,
      password: null,
    }
  },

  computed: {},

  watch: {
    $route() {
      this.pagination = {
        current: 1,
        size: 10,
        total: 0,
        searchKey: "",
        sortId: this.$route.query.sortId,
        labelId: this.$route.query.labelId,
        articleTitle: this.$route.query.articleTitle,
      };
      this.articles.splice(0, this.articles.length);
      this.sortId = this.$route.query.sortId;
      this.labelId = this.$route.query.labelId;
      this.articleTitle = this.$route.query.articleTitle;
      this.setTag();
      this.getArticles();
    }
  },

  created() {
    this.setTag();
    this.getArticles();
  },

  mounted() {
  },

  methods: {
    pageArticles() {
      this.pagination.current = this.pagination.current + 1;
      this.getArticles();
    },
    setTag() {
      if (!this.$common.isEmpty(this.sortId)) {
        let sortFilter = this.$store.state.sortInfo.filter(f => {
          return f.sortId === parseInt(this.sortId);
        });
        if (this.$common.isEmpty(this.labelId)) {
          let labelFilter = this.$store.state.labelInfo.filter(f => {
            return f.sortId === parseInt(sortFilter[0].sortId);
          });
          labelFilter.forEach(label => {
            let tagValue = {
              sortId: sortFilter[0].sortId,
              sortName: sortFilter[0].sortName,
              labelId: label.labelId,
              labelName: label.labelName
            };
            this.tag.push(tagValue);
          });
        } else {
          let labelFilter = this.$store.state.labelInfo.filter(f => {
            return f.labelId === parseInt(this.labelId);
          });
          let tagValue = {
            sortId: sortFilter[0].sortId,
            sortName: sortFilter[0].sortName,
            labelId: labelFilter[0].labelId,
            labelName: labelFilter[0].labelName
          };
          this.tag.push(tagValue);
        }
      }
    },
    labelArticle(labelId) {
      this.labelId = labelId;
      this.pagination = {
        current: 1,
        size: 10,
        total: 0,
        sortId: this.$route.query.sortId,
        labelId: labelId,
        articleTitle: this.$route.query.articleTitle,
      };
      this.articles.splice(0, this.articles.length);
      this.$nextTick(() => {
        this.getArticles();
      });
    },
    getArticles() {
      this.$createHttp.articlePage(this.pagination)
        .then((res) => {
          if (!this.$common.isEmpty(res.data)) {
            this.articles = this.articles.concat(res.data.records);
            this.pagination.total = res.data.total;
          }
        })
        .catch((error) => {
          this.$message({
            message: error,
            type: "error"
          });
        });
    },
    clickArticle(article) {
      if (article.encoderStatus) {
        this.showPasswordDialog = true;
        this.id = article.id;
      } else {
        this.$router.push({path: '/article', query: {id: article.id}})
      }
    },
    submitPassword() {
      this.$createHttp.checkPassword(this.id, this.password).then((res) => {
        if (res.code === 200 && res.data === true) {
          this.$router.push({path: '/article', query: {id: this.id}})
        } else {
          this.$message({
            message: "密码错误",
            type: "error"
          });
        }
      })
    }
  }
}
</script>

<style scoped>

.sort-warp {
  width: 70%;
  max-width: 780px;
  margin: 0 auto;
  padding: 20px;
  border-radius: 10px;
  display: flex;
  flex-wrap: wrap;
}

.article-wrap {
  width: 70%;
  margin: 40px auto;
  min-height: 600px;
}

.isActive {
  animation: scale 1.5s ease-in-out infinite;
}

.pagination-wrap {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}

.pagination {
  padding: 13px 15px;
  border: 1px solid var(--lightGray);
  border-radius: 3rem;
  color: var(--greyFont);
  width: 100px;
  user-select: none;
  cursor: pointer;
  text-align: center;
}

.pagination:hover {
  border: 1px solid var(--themeBackground);
  color: var(--themeBackground);
  box-shadow: 0 0 5px var(--themeBackground);
}

.article-first {
  color: var(--greyFont);
  border-bottom: 1px dashed var(--lightGray);
  padding-bottom: 5px;
  margin-bottom: 50px;
}

.recent-post-container {
  max-width: 780px;
  margin: auto;
}

.recent-post-container .recent-post-item:not(:last-child) {
  margin-bottom: 40px;
}

.recent-post-item {
  height: 300px;
  position: relative;
  display: flex;
  flex-direction: row;
  user-select: none;
  cursor: pointer;
  overflow: hidden;
  border-radius: 10px;
  animation: hideToShow 1s ease-in-out;
}

.recent-post-item-image {
  width: 50%;
  height: 100%;
}

.recent-post-item-image >>> .el-image__inner {
  transition: all 1s;
}

.recent-post-item-image >>> .el-image__inner:hover {
  transform: scale(1.2);
}

.leftImage {
  position: absolute;
  left: 0;
}

.rightImage {
  position: absolute;
  right: 0;
  text-align: right;
}

.recent-post-item-post {
  width: 50%;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px 35px;
}

.recent-post-item-post h3 {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

.post-meta {
  font-size: 12px;
  color: var(--greyFont);
}

.post-meta span:not(:last-child) {
  margin-right: 10px;
}

.recent-post-desc {
  font-size: 15px;
  line-height: 1.7;
  /* 超出4行就... */
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
}

.leftImage .sort-label {
  position: absolute;
  bottom: 20px;
}

.rightImage .sort-label {
  position: absolute;
  bottom: 20px;
  right: 35px;
}

.sort-label span {
  padding: 3px 10px;
  background-color: var(--maxLightGray);
  border-radius: 3px;
  font-size: 14px;
  color: var(--greyFont);
  transition: all 0.3s;
  cursor: pointer;
  user-select: none;
}

.sort-label span:hover {
  background-color: var(--themeBackground);
  color: var(--white);
}

.error-text {
  font-size: 20px;
  line-height: 1.8;
  letter-spacing: 8px;
  color: var(--white);
}

@media screen and (max-width: 700px) {
  .recent-post-item {
    height: 450px;
    position: unset;
    display: block;
    flex-direction: unset;
  }

  .recent-post-item-image {
    width: 100%;
    height: 200px;
  }

  .leftImage {
    position: unset;
    left: unset;
  }

  .rightImage {
    position: unset;
    right: unset;
    text-align: unset;
  }

  .recent-post-item-post {
    width: 100%;
    height: 250px;
    position: relative;
  }

  .recent-post-desc {
    -webkit-line-clamp: 3;
  }

  .leftImage .sort-label {
    position: absolute;
    bottom: 20px;
  }

  .rightImage .sort-label {
    position: absolute;
    bottom: 20px;
    right: unset;
  }
}


@media screen and (max-width: 900px) {
  .sort-warp {
    width: 90%;
  }

  .article-wrap {
    width: 90%;
  }
}
</style>
